﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Text;
using DAL;

public class uploadImg : IHttpHandler
{

    const string accessKeyId = "LTAIpBtfSVKiFD26";
    const string accessKeySecret = "AqLuVLBCME5iJ1EQkRryQ1ZqyhxLzh";
    const string endpoint = "http://oss-cn-beijing.aliyuncs.com";
    public void ProcessRequest(HttpContext context)
    {

        upimgoss();


    }

    //上传至阿里oss代码
    public void upimgoss()
    {
        if (HttpContext.Current.Request.Files.Count > 0)
        {

            HttpFileCollection files = HttpContext.Current.Request.Files;
            string imgCode = "0";
            string imgMsg = "出现异常了";
            string imgData = "";
            for (int i = 0; i < files.Count; i++)
            {
                HttpPostedFile file = files[i];               
                var fs = file.InputStream;
                OSSHelper OSS = new OSSHelper();
                OSS.PutObject("jhyyeye", "Web/" + file.FileName, fs);
                imgData += "{\"src\": \"" + "https://jhyyeye.oss-cn-beijing.aliyuncs.com/Web/" + file.FileName + "\",\"title\":\"" + file.FileName + "\"},";
                
                //下面为上传到本地代码
                //string fname = HttpContext.Current.Server.MapPath("~/Upload/articleimg/" + file.FileName);
                //imgData += "{\"src\": \"" + "/Upload/articleimg/" + file.FileName + "\",\"title\":\"" + file.FileName + "\"},";
                //file.SaveAs(fname);
            }
            imgData = imgData.Substring(0, imgData.Length - 1);
            HttpContext.Current.Response.ContentType = "text/plain";
            HttpContext.Current.Response.Write("{\"code\": \"" + imgCode + "\",\"msg\": \"" + imgMsg + "\" ,\"data\": " + imgData + "} ");
        }
    }
    
   





    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}